#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

using PdfCreative.Pdf.Writer;

namespace PdfCreative.Pdf.Graphics
{
    /// <summary>
    /// The Colour class is the base class for all colours used by graphics operations. The Colour
    /// class is abstract, and cannot be instantiated; use RgbColour or CymkColour instead.
    /// </summary>
    public abstract class Colour
    {
        // Private fields
        // ==============
        #region Private fields
        /// <summary>
        /// The colour components.
        /// </summary>
        protected byte[] _components;

        /// <summary>
        /// The colour space of this colour.
        /// </summary>
        protected ColourSpace _colourSpace;
        #endregion

        
     
        // Public properties
        // =================
        #region Public properties
        /// <summary>
        /// Retrieves the colour's colour space.
        /// </summary>
        public ColourSpace ColourSpace
        {
            get { return _colourSpace; }
        }
        #endregion



        // Public methods
        // ==============
        #region Public methods
        /// <summary>
        /// Clones the colour and returns it.
        /// </summary>
        /// <returns>The clone.</returns>
        public abstract Colour Clone();

        /// <summary>
        /// Retrieves the components of this colour.
        /// </summary>
        /// <returns>The components</returns>
        public IEnumerable<byte> GetComponents()
        {
            for (int c = 0; c < this.GetComponentLength(); c++)
                yield return _components[c];
        }

        /// <summary>
        /// Retrieves the number of components in this colour.
        /// </summary>
        /// <returns></returns>
        protected abstract int GetComponentLength();

        /// <summary>
        /// Writes an instruction to set the current non-stroking colour.
        /// </summary>
        /// <param name="writer">The PDF writer to use.</param>
        protected internal abstract void WriteNonStrokeColour(PdfWriter writer);

        /// <summary>
        /// Writes an instruction to set the current stroking colour.
        /// </summary>
        /// <param name="writer">The PDF writer to use.</param>
        protected internal abstract void WriteStrokeColour(PdfWriter writer);

        #endregion



        // Object overrides
        // ================
        #region Object overrides
        /// <summary>
        /// Indicates if the specified object is equal to the current colour. This is only the case if
        /// the other object is also a PdfColour, and the two colours are equal.
        /// </summary>
        /// <param name="obj">The other object.</param>
        /// <returns>A boolean indicating if the two colours are not equal (true).</returns>
        public override bool Equals(object obj)
        {
            Colour other = obj as Colour;

            bool result = true;

            if (this.ColourSpace == other.ColourSpace)
            {
                for (int component = 0; component < this._components.Length; component++)
                    if (this._components[component] != other._components[component])
                    {
                        result = false; break;
                    }
            }
            else
                result = false;

            return result;
        }

        /// <summary>
        /// Retrieves the hash code for the colour.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int hashcode = 0;

            int shift = 24;
            for (int component = 0; component < _components.Length; component++)
            {
                hashcode += _components[component] << shift;
                shift -= 8;
            }

            return hashcode;
        }
        #endregion



        // Public constructor
        // ==================
        #region Public constructor
        /// <summary>
        /// Constructs a new colour instance.
        /// </summary>
        protected Colour()
        {
            _components = new byte[4];
        }
        #endregion

    } // PdfColour structure
}
